<template>
  <div style="width: 30%; height: 400px; margin-top: 150px; margin-left: 550px;">
    <h3>用户注册</h3>
    <el-form ref="user" :model="user" label-width="80px" :rules="rules">
      <el-form-item label="用户名称" prop="username">
        <el-input v-model="user.username"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" prop="password">
        <el-input v-model="user.password"></el-input>
      </el-form-item>
      <el-form-item label="用户电话" prop="phone">
        <el-input v-model="user.phone"></el-input>
      </el-form-item>
      <el-form-item label="注册日期" prop="createTime">
        <!--value-format="yyyy-MM-dd" 格式化日期,解决日期少一天的问题-->
        <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="user.createTime"
                        style="width: 100%;"></el-date-picker>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="user.sex">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('user')">立即创建</el-button>
        <el-button @click="resetForm('user')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import a from "vuex-persistedstate";
import axios from "axios";

export default {
  name: "SignInByUserView",
  data() {
    return {
      user: {
        username: '',
        password: '',
        phone: '',
        createTime: '',
        money: 0.0,
        sex: '',
      },
      rules: {
        username: [
          {required: true, message: "请输入用户名", trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}

        ],
        password: [
          {required: true, message: "请输入密码", trigger: 'blur'}
        ],
        phone: [
          {required: true, message: "请输入电话号码", trigger: 'blur'}
        ],
        createTime: [
          {required: true, message: "请输入您的生日", trigger: 'blur'}
        ],
        sex: [
          {required: true, message: '请选择性别', trigger: 'change'}
        ],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //用户注册
          axios.post("/user/AddUser", this.user)
              .then((res) => {
                if (res.code === 2000) {//注册成功登录页
                  this.$message.success("注册成功!");
                  this.$router.push("/");
                } else {
                  this.$message.error("请检查输入的格式!");
                }
              })
              .catch((error) => {
                //请求失败
                this.$message.error("请求失败!" + error);
              })
          console.log(this.user)
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>